package dao;

import entity.Stock;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Created by jifeng on 2016/12/20 0020.
 */
public class StockDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    public  void  addStock(Stock stock){
        getCurrentSession().save(stock);
    }

    public Stock findProductById(int id){
        String sql="SELECT * FROM Stock s WHERE s.productId="+id+"";
        Stock stock=(Stock) getCurrentSession().createSQLQuery(sql).addEntity(Stock.class).list().get(0);
        return stock;
    }

    public void updateStock(Stock stock){
        String sql ="UPDATE stock s SET s.stocks ="+stock.getStocks()+" WHERE s.id="+stock.getId()+"";
        getCurrentSession().createSQLQuery(sql).executeUpdate();
    }
}
